package file.a01;

import java.io.File;
import java.io.IOException;

public class Exercise01 {
    public static void main(String[] args) throws IOException {
        String fileName="czk.txt";
        //创建文件
        createFile(fileName);
        printInfo(fileName);
        //删除文件
        deleteFile(fileName);
        //打印系统根目录
        printRoot();
    }
    //创建文件
    public static void createFile(String fileName) throws IOException {
        File f=new File(fileName);
        if(!f.exists()){
            f.createNewFile();
        }
    }
    //删除文件
    public static void deleteFile(String fileName){
        File f=new File(fileName);
        if(f.exists()){
            f.delete();
        }
    }
    //打印系统所有根目录
    public static void printRoot(){
       File[] roots=File.listRoots();
       for(File root:roots){
           System.out.println(root.getAbsoluteFile());
       }
    }
    //打印文件信息
    public static void printInfo(String fileName) throws IOException {
        File f=new File(fileName);
        System.out.println("打印全局路径："+f.getAbsolutePath());
        System.out.println("打印文件全局路径："+f.getAbsoluteFile());
        System.out.println("打印父路径："+f.getParent());
        System.out.println(f.exists());
        //打印当前目录下的所有文件
        File path=new File(".");
        for(File parentFile:path.listFiles()){
            System.out.println("name:"+parentFile.getName());
        }
    }
    //创建临时文件
    public static void createTmpFile(String tmpName) throws IOException {
        File path=new File(".");
        File tmpFile=File.createTempFile(tmpName,".tmp",path);
        tmpFile.createNewFile();
        tmpFile.deleteOnExit();
    }
}
